package com.tianqu.tms.www.check.controller;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsController;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.exception.TransactionException;
import com.tianqu.tms.core.security.TmsUserProfile;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.entity.Attn;
import com.tianqu.tms.entity.IntegralRegister;
import com.tianqu.tms.entity.Member;
import com.tianqu.tms.entity.TransferAcceptedInfo;
import com.tianqu.tms.entity.TransferFinanceInfo;
import com.tianqu.tms.entity.TransferProcessHistory;
import com.tianqu.tms.www.check.action.CheckCarPrintAjaxAction;

/**
 * @author zhangxueliang 流程表打印controller
 */
public class CheckCarPrintAjaxController extends TmsController {
	private Logger logger = Logger.getLogger(CheckCarPrintAjaxController.class);

	@Override
	public void execute(UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		TxController controller = TxController.getTxController();

		// 添加事务提交异常捕获
		try {
			controller.begin();
			String serial = (String) uc.getAttribute(CheckCarPrintAjaxAction.SERIAL);
			String ownerName = (String) uc.getAttribute(CheckCarPrintAjaxAction.OWNERNAME);
			String ownerType = (String) uc.getAttribute("ownerType");
			String sourceType = (String) uc.getAttribute("sourceType");
			String cardType = (String) uc.getAttribute("cardType");
			String licensePlate = (String) uc.getAttribute("licensePlate");
			String checkPrice = (String) uc.getAttribute("checkPrice");
			String transferPrice = (String) uc.getAttribute("transferPrice");
			String remark = (String) uc.getAttribute("remark");
			String accessCardNo = (String) uc.getAttribute("accessCardNo");
			String vin = (String) uc.getAttribute("vin");

			logger.debug("CheckCarPrintAjaxController,serial:"+serial+"ownerName:"+ownerName+"ownerType:"+ownerType+"sourceType:"+sourceType+"cardType:"+cardType+"licensePlate:"+licensePlate+"checkPrice:"+checkPrice
					+"transferPrice:"+transferPrice+"remark:"+remark+"accessCardNo:"+accessCardNo+"vin:"+vin);
			
			uc.removeAttribute(CheckCarPrintAjaxAction.SERIAL);
			uc.removeAttribute(CheckCarPrintAjaxAction.OWNERNAME);
			uc.removeAttribute("ownerType");
			uc.removeAttribute("sourceType");
			uc.removeAttribute("cardType");
			uc.removeAttribute("transferPrice");
			uc.removeAttribute("carLicense");
			uc.removeAttribute("checkPrice");
			uc.removeAttribute("remark");
			TmsUserProfile profile = uc.getUserProfile();

			//----
			Member member = new Member();
			Attn attn = new Attn();
			member = member.getIntegralManagementInfo(accessCardNo);
			attn = attn.getIntegralInfo(accessCardNo);
			
			//--
			TransferAcceptedInfo acc = new TransferAcceptedInfo();
			acc = acc.getAcceptInfoBySerial(serial, "");
			//
			TransferAcceptedInfo transferAcceptedInfo = new TransferAcceptedInfo();
			transferAcceptedInfo.setSerial(serial);
			transferAcceptedInfo.setLc_printNum(acc.getLc_printNum()+1);
			transferAcceptedInfo.setCy_printNum(acc.getCy_printNum());
			transferAcceptedInfo.setUpdateUserId(profile.getUniqueId());//setLc_printUserId
			transferAcceptedInfo.setCarOwner(ownerName);
			transferAcceptedInfo.setOwnerType(Integer.parseInt(ownerType));
			transferAcceptedInfo.setStatus(2);
			transferAcceptedInfo.setSourceType(Integer.parseInt(sourceType));
			transferAcceptedInfo.setCy_cardType(cardType);
			transferAcceptedInfo.setLicensePlate(licensePlate);
			transferAcceptedInfo.setVin(vin);
			if (StringUtil.isNotBlank(transferPrice)) transferAcceptedInfo.setTransferPrice(Integer.parseInt(transferPrice));
			else transferAcceptedInfo.setTransferPrice(0);
			if (member != null) transferAcceptedInfo.setMemberId(String.valueOf(member.getMemId()));
			else transferAcceptedInfo.setMemberId(acc.getMemberId());
			if (attn != null) transferAcceptedInfo.setAttnId(attn.getAttnId());
			else transferAcceptedInfo.setAttnId(acc.getAttnId());
			if (cardType.equals(0)) transferAcceptedInfo.setLicensePlateCol("2");
			else if (cardType.equals(1)) transferAcceptedInfo.setLicensePlateCol("1");
			transferAcceptedInfo.updateTransferLcPrintNumBySerial(transferAcceptedInfo);
			
			//----
			//# 业务类型为“2：流程受理（即打印流程表）”
			if (acc.getLc_printNum() == 0) {
				TransferProcessHistory transferProcessHistory = TransferProcessHistory.getInstance();
				transferProcessHistory.setBusinessType(2);
				transferProcessHistory.setSerial(serial);
				transferProcessHistory.setInsertUserId(profile.getUniqueId());
				transferProcessHistory.setReason(remark);
				transferProcessHistory.insertHisInfoAtAssess(transferProcessHistory);//1

				if (StringUtil.isNotBlank(transferPrice)) {
					TransferProcessHistory transferProcessHistory1 = TransferProcessHistory.getInstance();
					transferProcessHistory1.setSerial(serial);
					transferProcessHistory1.setBusinessType(8);
					transferProcessHistory1.setInsertUserId(profile.getUniqueId());
					transferProcessHistory1.setReason(remark);
					transferProcessHistory1.insertHisInfoAtAssess(transferProcessHistory1);//2
				}
			}
			
			//
			
			TransferFinanceInfo finance = new TransferFinanceInfo();
			finance = finance.findFinanceInfoByserial(serial);
			finance.setSerial(serial);
			//
			if (finance.getCheckPrice()>0) finance.setCheckPriceUserId(1);
			else  finance.setCheckPriceUserId(profile.getUniqueId());
			//
			if (StringUtil.isNotBlank(checkPrice)) finance.setCheckPrice(Integer.parseInt(checkPrice));
			else if(finance != null) finance.setCheckPrice(finance.getCheckPrice());
		    //
			if (StringUtil.isNotBlank(transferPrice)) finance.setTransferPrice(Integer.parseInt(transferPrice));
			else if(finance != null) finance.setTransferPrice(finance.getTransferPrice());
			// 
			if(StringUtil.isNotBlank(accessCardNo)){
				finance.setCheckPriceChargeType(3);
				finance.setTransferPriceChargeType(3);
			}else if(finance != null){
				finance.setCheckPriceChargeType(finance.getCheckPriceChargeType());
				finance.setTransferPriceChargeType(finance.getTransferPriceChargeType());
			}
			finance.setTransferPriceUserId(profile.getUniqueId());
			finance.updateFinanceInfoAtCheck(finance);

			//--
			int p1 = StringUtil.isNullOrBlank(checkPrice) ? 0 : Integer.parseInt(checkPrice);
			int p2 = StringUtil.isNullOrBlank(transferPrice) ? 0 : Integer.parseInt(transferPrice);
			int price = p1 + p2;//页面输入的金额
			if (!StringUtil.isNullOrBlank(accessCardNo) && (member != null || attn != null)) {
				if (member != null) {
					int virtualMargin = Integer.parseInt(member.getVirtualMargin()) - price;
					member.setAccessCardNo(accessCardNo);
					member.setVirtualMargin(virtualMargin + "");// 过户费余额
					member.setRealMargin(member.getRealMargin());// 充值过户费金额
					member.setGiftMargin(member.getGiftMargin());// 赠送过户费金额
					member.setUpdateUserId(profile.getUniqueId());
					member.updateMarginInfo(member);
				} else if (attn != null) {
					int virtualMargin = attn.getVirtualMargin() - price;
					attn.setAccessCardNo(accessCardNo);
					attn.setVirtualMargin(virtualMargin);// 过户费余额
					attn.setRealMargin(attn.getRealMargin());// 充值过户费金额
					attn.setGiftMargin(attn.getGiftMargin());// 赠送过户费金额
					attn.setUpdateUserId(profile.getUniqueId());
					attn.updateMarginInfo(attn);
				}

				//
				IntegralRegister register = new IntegralRegister();
				if (member != null) {
					register.setMemberId(String.valueOf(member.getMemId()));
					register.setMemberType(1);
				} else if (attn != null) {
					register.setMemberId(attn.getAttnId());
					register.setMemberType(2);
				}
				register.setSerial(serial);
				register.setLicensePlate(licensePlate);
				register.setTransferPrice(price);
				register.setIntegral(0);
				register.setRemark("过户费缴费");
				register.setInsertUserId(profile.getUniqueId());
				register.setPlusMinus(2);
				register.setChargeType(12);
				register.insertRegisterInfo(register);
			}

			controller.commit();
		} catch (Exception e) {
			logger.error(e);
			try {
				controller.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR, e1);
			}
			// 系统异常
			if (e instanceof TmsException) {
				throw e;
			}
			throw new TmsException(TmsException.DB_TXCRROR, e);
		}

	}

}
